package com.yxkj.syh.app.huarong.activities.links.save;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.syh.app.basic.base.BaseActivity;
import com.syh.app.basic.utils.Tooast;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.ActivityEditLinkBinding;
import com.yxkj.syh.app.huarong.views.AddressDialog;

/**
 * 保存联系人 添加联系人 编辑联系人 修改联系人
 */
@Route(path = ArouterPath.EDIT_LINK_ACTIVITY)
public class EditLinkActivity extends BaseActivity<ActivityEditLinkBinding, EditLinkVM> {

    @Autowired
    long id;
    @Autowired
    boolean isOrderRequested = false;

    private AddressDialog addressDialog;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_edit_link;
    }

    @Override
    protected int getViewModelId() {
        return BR.editLinkVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        mVDBinding.titleView.setLeftOnClickListener(v -> {
            finish();
        });

        mVDBinding.llArea.setOnClickListener(v -> {
            if (addressDialog == null) {
                addressDialog = new AddressDialog(EditLinkActivity.this);
                addressDialog.setOnAddressSelectedListener((province, city, county, street) -> {
                    addressDialog.cancel();
                    StringBuffer addressBuffer = new StringBuffer();
                    addressBuffer.append(province.name);
                    if (city != null) {
                        addressBuffer.append(city.name);
                        if (county != null) {
                            addressBuffer.append(county.name);
                            if (street != null) {
                                addressBuffer.append(street.name);
                            }
                        }
                    }
                    mViewModel.ofForm4.set(addressBuffer.toString());
                });
                addressDialog.getSelector().setAddressProvider(mViewModel.addressProvider);
            }
            addressDialog.show();
        });

        mVDBinding.llPayment.setOnClickListener(v -> ARouter.getInstance().build(ArouterPath.BANK_CARDS_ACTIVITY).withLong("id", id).navigation());

        if (id > 0) {
            mVDBinding.titleView.setTitleText("修改供应商");
        } else {
            mVDBinding.titleView.setTitleText("添加供应商");
        }
    }

    @Override
    protected void initData() {
        if (id > 0) {
            mViewModel.linkmanInfo(id);
//            mViewModel.ofMainInfoEditEnable.set(false);
        }
        mViewModel.setOrderRequested(isOrderRequested);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldSaveSuccess.observe(this, linkman -> {
            if (mViewModel.getLinkman() == null) {
                new AlertDialog.Builder(this).setMessage("是否添加收款信息")
                        .setNegativeButton("不了", (dialog, which) -> {
                            dialog.cancel();
                            finish();
                        })
                        .setPositiveButton("去添加", (dialog, which) -> {
                            dialog.cancel();
                            ARouter.getInstance().build(ArouterPath.BANK_CARDS_ACTIVITY).withLong("id", linkman.getId()).navigation();
                            finish();
                        }).create().show();
//                ARouter.getInstance().build(ArouterPath.LINK_DETAIL_ACTIVITY).withLong("id", linkman.getId())
//                        .withBoolean("isFirst", true).navigation();
            } else {
                if (isOrderRequested) {
                    Intent intent = new Intent();
                    intent.putExtra("data", linkman);
                    setResult(RESULT_OK, intent);
                } else {
                    ARouter.getInstance().build(ArouterPath.LINK_DETAIL_ACTIVITY).withLong("id", linkman.getId())
                            .navigation();
                    Tooast.success("修改成功");
                }
                finish();
            }
        });
    }
}
